package org.itboys.mobile.service.mongo.mobile;

import org.itboys.admin.dto.lasted.SundayUploadDto;
import org.itboys.admin.entity.lasted.SundayUpload;
import org.itboys.admin.service.lasted.SundayUploadService;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dto.SundayCommentDto;
import org.itboys.mobile.entity.mongo.SundayAdvertisement;
import org.itboys.mobile.entity.mongo.SundayComment;
import org.itboys.mobile.entity.mongo.cookbook.KitchenCookBook;
import org.itboys.mobile.entity.mysql.product.SundayProduct;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_评论_service_移动端
 */
@Service
public class SundayMobileCommentService extends BaseMongoService<SundayComment>{
    @Autowired
    private SundayUploadService uploadService;
    @Autowired
    private ResourceHolder resourceHolder;
    @Resource(name = "mobileDs")
    private MongoDataSource sundayDataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return sundayDataSource;
    }

    @Override
    protected Class<SundayComment> getEntityClass() {
        return SundayComment.class;
    }

    /**
     * 分页获取评论
     * @param memberId
     * @return
     */
    public List<SundayCommentDto> getCommentByPage(HttpServletRequest request,Long memberId, Integer type, Long outClassId){
        Map<String,Object> param =new HashMap<String,Object>();
        //获取商品评论
        if(type==1){
            param.put("outClassId",outClassId);
            param.put("outClassName",SundayProduct.class.getSimpleName());
        }
        //获取资讯评论
        if(type==2){
            param.put("outClassId",outClassId);
            param.put("outClassName", SundayAdvertisement.class.getSimpleName());
        }
        //获取菜谱评论
        if(type==3){
            param.put("outClassId",outClassId);
            param.put("outClassName", KitchenCookBook.class.getSimpleName());
        }
        param.put("isDeleted", SundayCommonConstants.NO);
        //按评论生成时间，倒叙排序
        param.put("sort","ct");
        param.put("order","DESC");

        return packageDtoMuilt(super.pageQuery(request,param).getData());
    }
    /**
     * 获取指定数量评论。一般适用于商品详情中
     * @param number
     * @return
     */
    public List<SundayCommentDto> getCommentByNumber(Integer number,Long outClassId,String outClassName){
        logger.info("开始组装commentDto-2");
        Map<String,Object> param =new HashMap<String, Object>();

        param.put("outClassId",outClassId);
        param.put("outClassName",outClassName);
        param.put("isDeleted", SundayCommonConstants.NO);
        //按评论生成时间，倒叙排序
        param.put("sort","ct");
        param.put("order","DESC");
        List<SundayCommentDto> dtos =packageDtoMuilt(super.list(param,2));
        return dtos;
    }

    /**
     * 生成评论信息，支持批量。单个评论，可以对应三张图片。逻辑可以自定义
     * @param outClassIds
     * @param contents
     * @param scores
     * @param images 图片集合，固定大小=productIds集合大小*3。。每条评论限制最大传入三张，没有图片的使用noImage字符传固定值填充占位。
     * @param memberId
     * @param memberImage
     * @param memberName
     * @param outClassName
     */
    public void generateComment(List<Long> outClassIds,
                                List<String> contents,
                                List<Integer> scores,
                                List<String> images,
                                Long memberId,
                                String memberImage,
                                String memberName,
                                String outClassName ){
        //保存评论
        List<SundayComment> comments =new ArrayList<SundayComment>();
        for(int i=0;i<outClassIds.size();i++){
            Long  outClassId=outClassIds.get(i);
            String content =contents.get(i);
            Integer  score=scores.get(i);

            comments.add(new SundayComment(outClassId, SundayProduct.class.getSimpleName(), memberId, memberName,
                    memberImage, score, content));
        }
        super.batchSave(comments);
        //保持评论图片
        List<SundayUpload> uploads=new ArrayList<SundayUpload>();
        for(int i=0;i<comments.size();i++){
           SundayComment comment=comments.get(i);
           //循环单挑评论图片，固定大小：3。可以修改此变量
           for(int j=0;j<3;j++){
                String savePath =    images.get(i*3+j);
                if(!savePath.contains("noImage")){
                    SundayUpload upload = new SundayUpload(comment.getId(), SundayComment.class.getSimpleName(),
                            savePath, "评论"+comment.getId()+"图片", null, null);
                    uploads.add(upload);
                }
           }
        }
        uploadService.batchSave(uploads);
    }
    /**
     * 组装评论DTO。逻辑可以自定义
     */
    public SundayCommentDto packageDtoSingle(SundayComment comment,List<SundayUploadDto> images,boolean isPackageImages) {
        if(isPackageImages){
            images =uploadService.selectNoPage(comment.getId(),SundayComment.class.getSimpleName());
        }
        String imgRoot=resourceHolder.getStringValue("imgRoot");
        return new SundayCommentDto(comment.getId(), comment.getCreateTime(), comment.getCreateDate(),
                comment.getMemberId(), comment.getMemberName(), imgRoot+comment.getMemberImage(), comment.getScore(),
                comment.getContent(), images);
    }
    public List<SundayCommentDto>  packageDtoMuilt(List<SundayComment> comments) {

        List<SundayCommentDto> dtos = new ArrayList<SundayCommentDto>();
        List<Long> outClassIds = new ArrayList<Long>();
        for(SundayComment comment:comments){
            outClassIds.add(comment.getId());
        }
       // List<SundayUploadDto> allImages=  new ArrayList<SundayUploadDto>();
        List<SundayUpload> allImages=  new ArrayList<SundayUpload>();
        if(ListUtil.isNotNull(outClassIds)){
            Map<String,Object> param =new HashMap<String, Object>();
            param.put("isDeleted", SundayCommonConstants.NO);
            param.put("outClassId in",outClassIds);
            param.put("outClassName",SundayComment.class.getSimpleName());
            allImages= uploadService.list(param);
            for(SundayUpload allImage:allImages){
            }
        }
        for(SundayComment comment:comments){
            List<SundayUpload> images = new ArrayList<SundayUpload>();

            for(SundayUpload allImage:allImages){
                if(LongUtil.isNotZreo(allImage.getOutClassId())&&
                        comment.getId()==allImage.getOutClassId().longValue()){

                    images.add(allImage);
                }
            }
            logger.info("images--------------------size="+images.size());
            dtos.add(packageDtoSingle(comment,uploadService.packageDtoMuilt(images),false));

        }
        return dtos;
    }
}
